import Decorator from '../core/Decorator';
import Tick from '../core/Tick';
import BaseNode from '../core/BaseNode';
import { FAILURE } from '../constants';
// 进行数据的前置判断
export default abstract class Checker<T> extends Decorator {

  constructor(child: BaseNode) {
    super(`Checker[${child.name}]`, child);
  }

  onTick(tick: Tick, ...args: any[]) {
    let result: T
    try {
      result = this.onCheck(tick, ...args)
    } catch (e) {
      console.log('checker error ', e)
      return FAILURE
    }
    if (result === null || result === undefined) {
      return FAILURE
    }
    if (Array.isArray(result)) {
      return this.child.execute(tick, ...args, ...result);
    }
    return this.child.execute(tick, ...args, result);
  }

  abstract onCheck(tick: Tick, ...args: any[]): T
};
